package gw.com.android.ui.positions;

import android.os.Bundle;
import android.view.View;

import com.gwtsz.android.rxbus.RxBus;
import com.gwtsz.gts2.hx.R;

import gw.com.android.app.ActivityManager;
import gw.com.android.app.AppContances;
import gw.com.android.app.AppMain;
import gw.com.android.model.ConfigType;
import gw.com.android.model.ConfigUtil;
import gw.com.android.model.DataManager;
import gw.com.android.terminal.GTSDataListener;
import gw.com.android.ui.MainActivity;
import gw.com.android.ui.dialog.PopupConfirmDialog;
import gw.com.jni.library.terminal.GTSConst;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import www.com.library.app.Logger;
import www.com.library.app.ObjectSessionStore;
import www.com.library.model.DataItemDetail;

/**
 * 持仓界面
 */
public class PositionFragment extends SubFragment {

    public PositionFragment() {
        // Required empty public constructor
    }

    public static PositionFragment newInstance() {
        PositionFragment fragment = new PositionFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {

        }
    }

    @Override
    protected void initViewData() {
        mCurrentTag = ConfigType.TAB_POSITION_TYPE;
        mAdapter = new PositionAdapter(getActivity(), mListView);
        mListView.setAdapter(mAdapter);
        mListView.setVisibility(View.VISIBLE);
    }

    @Override
    public void setEmptyView() {
        mEmptyView.setText(AppMain.getAppString(R.string.order_position_no_date));
        mCreateOrder.setVisibility(View.VISIBLE);
        mCreateOrder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                DataItemDetail model = DataManager.instance().getTradeTickModel();
//                if (null != model) {
//                    ActivityManager.showOrderActivity(getActivity(), model, AppContances.TRADE_TYPE_BUY, AppContances.TRADE_TYPE_FORM_TRADE);
//                } else {
//                    final PopupConfirmDialog dialog = PopupConfirmDialog.newInstance(getActivity(), "", ConfigUtil.instance().getErrorConfigObject().optString(ConfigType.SERVER_ERROR_1021));
//                    dialog.show();
//                }//不要弹窗啦，直接跳到行情tab就行了
                MainActivity mainActivity = (MainActivity) ObjectSessionStore.getObject(MainActivity.class.getSimpleName() + GTSDataListener.mainNum);
                mainActivity.setTabFragment(ConfigType.TAB_QUOTE_TAG, "");
            }
        });
    }

    @Override
    public void registerRxBus() {
        Disposable positionListRegister = RxBus.getInstance().registertoObservableSticky(GTSConst.REPLY_POSTION_LIST + "", Bundle.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bundle>() {

                    @Override
                    public void accept(@NonNull Bundle bundle) throws Exception {
                        Logger.i("持仓列表刷新MainActivity 持仓列表回包接收到回调");
                        refreshViewData();
                    }
                });
        bindSubscription(positionListRegister);

        Disposable positionUpdateListRegister = RxBus.getInstance().register(GTSConst.REPLY_POSTION_UPDATELIST + "", Bundle.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bundle>() {

                    @Override
                    public void accept(@NonNull Bundle bundle) throws Exception {
                        if (null != bundle) {
                            if (null != mListView && !mListView.mIsOnTouch && !mListView.mIsScrolling) {
                                if (null != mAdapter) {
                                    mAdapter.refreshPrice(bundle.getIntegerArrayList("object"));
                                }
                            }
                        }
                    }
                });
        bindSubscription(positionUpdateListRegister);

        Disposable positionUpdateRegister = RxBus.getInstance().register(GTSConst.REPLY_POSTION_UPDATE + "", Bundle.class).observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Bundle>() {

                    @Override
                    public void accept(@NonNull Bundle bundle) throws Exception {
                        if (null != bundle) {
                            refreshOrder(bundle.getInt("iNotification"));
                        }
                    }
                });
        bindSubscription(positionUpdateRegister);
    }

}
